
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>线程和线程池 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="block-close.html" />
    
    
    <link rel="prev" href="../concurrent.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">Java</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../object-mgmr.html">
            
                <a href="../object-mgmr.html">
            
                    
                    对象管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../enum.html">
            
                <a href="../enum.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../object-common.html">
            
                <a href="../object-common.html">
            
                    
                    对象通用行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../concurrent.html">
            
                <a href="../concurrent.html">
            
                    
                    线程和并发
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.5.1" data-path="how-to.html">
            
                <a href="how-to.html">
            
                    
                    线程和线程池
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="block-close.html">
            
                <a href="block-close.html">
            
                    
                    线程阻塞和关闭
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="communicate.html">
            
                <a href="communicate.html">
            
                    
                    线程协作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="volatile.html">
            
                <a href="volatile.html">
            
                    
                    活性失败-volatile
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="lock.html">
            
                <a href="lock.html">
            
                    
                    安全性失败-锁
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="thread-local.html">
            
                <a href="thread-local.html">
            
                    
                    线程局部存储
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="others.html">
            
                <a href="others.html">
            
                    
                    其他
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../collections.html">
            
                <a href="../collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../nio.html">
            
                <a href="../nio.html">
            
                    
                    nio
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../advance.html">
            
                <a href="../advance.html">
            
                    
                    泛型，注解和反射
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../mem-gc.html">
            
                <a href="../mem-gc.html">
            
                    
                    内存模型和GC
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../classloader.html">
            
                <a href="../classloader.html">
            
                    
                    ClassLoader
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Web</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../spring.html">
            
                <a href="../spring.html">
            
                    
                    spring
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">GUI</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../gui-1.html">
            
                <a href="../gui-1.html">
            
                    
                    没事写个小界面什么的
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >线程和线程池</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x7EBF;&#x7A0B;&#x548C;&#x7EBF;&#x7A0B;&#x6C60;">&#x7EBF;&#x7A0B;&#x548C;&#x7EBF;&#x7A0B;&#x6C60;</h1>
<h2 id="1-java&#x7684;&#x7EBF;&#x7A0B;">1 Java&#x7684;&#x7EBF;&#x7A0B;</h2>
<h3 id="11-&#x7EBF;&#x7A0B;&#x548C;&#x7EBF;&#x7A0B;&#x6C60;">1.1 &#x7EBF;&#x7A0B;&#x548C;&#x7EBF;&#x7A0B;&#x6C60;</h3>
<p>Java&#x7684;&#x7EBF;&#x7A0B;&#x5C31;&#x662F;Thread&#xFF0C;&#x4E00;&#x4E2A;Thread&#x5BF9;&#x8C61;&#xFF0C;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;<br>Runnable&#x662F;&#x4E00;&#x4E2A;&#x63A5;&#x53E3;&#xFF0C;&#x7ED9;Thread&#x63D0;&#x4F9B;&#x4EFB;&#x52A1;</p>
<p>&#x8FD9;&#x4E24;&#x4E2A;&#x5C31;&#x662F;Java&#x91CC;&#x6700;&#x57FA;&#x672C;&#x7684;&#x7EBF;&#x7A0B;</p>
<pre><code>new Thread(Runnable, &quot;thread-name&quot;).start();
</code></pre><p>&#x4F46;&#x7EBF;&#x7A0B;&#x5BF9;&#x4E8E;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x6765;&#x8BF4;&#xFF0C;&#x662F;&#x4E00;&#x79CD;&#x8D44;&#x6E90;&#xFF0C;&#x65E2;&#x7136;&#x662F;&#x8D44;&#x6E90;&#xFF0C;&#x5C31;&#x4E0D;&#x80FD;&#x65E0;&#x9650;&#x4F7F;&#x7528;&#xFF0C;&#x540C;&#x65F6;&#x7EBF;&#x7A0B;&#x4E5F;&#x662F;&#x4E00;&#x79CD;&#x91CD;&#x91CF;&#x7EA7;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x521D;&#x59CB;&#x5316;&#x4E5F;&#x662F;&#x4E2A;&#x8D39;&#x4E8B;&#x7684;&#x5DE5;&#x7A0B;&#xFF0C;&#x6240;&#x4EE5;&#x4E3A;&#x4E86;&#x63A7;&#x5236;&#x7EBF;&#x7A0B;&#x7684;&#x6570;&#x91CF;&#xFF0C;&#x5E76;&#x4E14;&#x5BF9;&#x5DF2;&#x7ECF;&#x521D;&#x59CB;&#x5316;&#x8FC7;&#x7684;&#x7EBF;&#x7A0B;&#x8FDB;&#x884C;&#x91CD;&#x7528;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x7528;&#x5230;&#x7EBF;&#x7A0B;&#x6C60;&#xFF0C;Java&#x4E3A;&#x6211;&#x4EEC;&#x63D0;&#x4F9B;&#x4E86;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x5C31;&#x662F;Executor&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x770B;&#x770B;Java&#x7684;&#x7EBF;&#x7A0B;&#x6C60;&#xFF1A;  </p>
<p>Executor&#xFF1A;&#x4E00;&#x4E2A;&#x63A5;&#x53E3;&#xFF0C;&#x5176;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x63A5;&#x6536;Runnable&#x5BF9;&#x8C61;&#x7684;&#x65B9;&#x6CD5;executor&#xFF0C;&#x5176;&#x65B9;&#x6CD5;&#x7B7E;&#x540D;&#x4E3A;executor(Runnable command)  </p>
<p>Executors&#xFF1A;&#x63A7;&#x5236;&#x7740;&#x4E00;&#x5806;&#x7EBF;&#x7A0B;&#x6C60;  </p>
<p>ExecutorService&#xFF1A;&#x7EE7;&#x627F;Executor&#xFF0C;&#x8FDB;&#x884C;&#x4E86;&#x6269;&#x5C55;&#xFF0C;&#x5982;&#x5BF9;Callable&#x548C;Future&#x7684;&#x652F;&#x6301;&#xFF0C;shutdown&#x7B49;&#xFF0C;&#x8FD9;&#x4E2A;&#x662F;&#x5177;&#x6709;&#x670D;&#x52A1;&#x751F;&#x547D;&#x5468;&#x671F;&#x7684;Executor<br>&#x4F8B;&#x5982;&#x5173;&#x95ED;&#xFF0C;&#x8FD9;&#x4E1C;&#x897F;&#x77E5;&#x9053;&#x5982;&#x4F55;&#x6784;&#x5EFA;&#x6070;&#x5F53;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x6765;&#x6267;&#x884C;Runnable&#x5BF9;&#x8C61;&#xFF0C;&#x662F;&#x4E00;&#x4E2A;&#x6BD4;Executor&#x4F7F;&#x7528;&#x66F4;&#x5E7F;&#x6CDB;&#x7684;&#x5B50;&#x7C7B;&#x63A5;&#x53E3;&#xFF0C;&#x5176;&#x63D0;&#x4F9B;&#x4E86;&#x751F;&#x547D;&#x5468;&#x671F;&#x7BA1;&#x7406;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x4EE5;&#x53CA;&#x53EF;&#x8DDF;&#x8E2A;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x5F02;&#x6B65;&#x4EFB;&#x52A1;&#x6267;&#x884C;&#x72B6;&#x51B5;&#x8FD4;&#x56DE;Future&#x7684;&#x65B9;&#x6CD5;  </p>
<p>AbstractExecutorService&#xFF1A;&#x5B9E;&#x73B0;&#x4E86;ExecutorService&#xFF0C;&#x4F46;&#x6CA1;&#x6709;&#x5B9E;&#x73B0;executor&#x65B9;&#x6CD5;  </p>
<p>ThreadPoolExecutor&#xFF1A;&#x5B9E;&#x73B0;&#x4E86;executor&#x65B9;&#x6CD5;&#xFF0C;&#x8FD9;&#x624D;&#x662F;&#x771F;&#x6B63;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;&#xFF0C;Executors&#x91CC;&#x7684;&#x5404;&#x79CD;&#x7EBF;&#x7A0B;&#x6C60;&#xFF0C;&#x5176;&#x5B9E;&#x5C31;&#x662F;&#x8FD9;&#x4E2A;&#x7C7B;&#x7684;&#x4E0D;&#x540C;&#x914D;&#x7F6E;  </p>
<p>ScheduledExecutorService&#xFF1A;&#x4E00;&#x4E2A;&#x63A5;&#x53E3;&#xFF0C;&#x7EE7;&#x627F;&#x81EA;ExecutorService, &#x4E00;&#x4E2A;&#x53EF;&#x5B9A;&#x65F6;&#x8C03;&#x5EA6;&#x4EFB;&#x52A1;&#x7684;&#x63A5;&#x53E3;  </p>
<p>ScheduledThreadPoolExecutor&#xFF1A;ScheduledExecutorService&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x7236;&#x7C7B;&#x662F;ThreadPoolExecutor&#xFF0C;&#x4E00;&#x4E2A;&#x53EF;&#x5B9A;&#x65F6;&#x8C03;&#x5EA6;&#x4EFB;&#x52A1;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;</p>
<p>&#x7528;&#x6CD5;&#xFF1A;Executors&#x7684;&#x6BCF;&#x4E2A;&#x65B9;&#x6CD5;&#x90FD;&#x53EF;&#x4EE5;&#x4F20;&#x5165;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x4E00;&#x4E2A;ThreadFactory&#x5BF9;&#x8C61;</p>
<pre><code>ExecutorService exec = Executors.newCachedThreadPool(); //&#x7EBF;&#x7A0B;&#x6570;&#x603B;&#x662F;&#x4F1A;&#x6EE1;&#x8DB3;&#x6240;&#x6709;&#x4EFB;&#x52A1;&#xFF0C;&#x6240;&#x6709;&#x4EFB;&#x52A1;&#x90FD;&#x662F;&#x5E76;&#x884C;&#x6267;&#x884C;&#xFF0C;&#x540C;&#x65F6;&#x62A2;&#x65F6;&#x95F4;&#x7247;&#xFF0C;&#x800C;&#x65E7;&#x7EBF;&#x7A0B;&#x4F1A;&#x88AB;&#x7F13;&#x5B58;&#x548C;&#x590D;&#x7528;
ExecutorService exec = Executors.newFixedThreadPool(2); //&#x4E24;&#x4E2A;&#x7EBF;&#x7A0B;&#x540C;&#x65F6;&#x8FD0;&#x884C;&#xFF0C;&#x5176;&#x4ED6;&#x7684;&#x4F1A;&#x6392;&#x961F;&#x7B49;&#x5F85;
ExecutorService exec = Executors.newSingleThreadExecutor(); //1&#x4E2A;&#x7EBF;&#x7A0B;&#x540C;&#x65F6;&#x8FD0;&#x884C;&#xFF0C;&#x5373;&#x591A;&#x4E2A;&#x4EFB;&#x52A1;&#x4F1A;&#x4E32;&#x884C;&#x6267;&#x884C;
ExecutorService exec = Executors.newWorkStealingPool();  //&#x4E0D;&#x77E5;&#x9053;&#x5565;&#x610F;&#x601D;
ScheduledExecutorService exec = Executors.newScheduledThreadPool(10);//&#x4E0D;&#x77E5;&#x9053;&#x5565;&#x610F;&#x601D;
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();//&#x4E0D;&#x77E5;&#x9053;&#x5565;&#x610F;&#x601D;

for(int i = 0; i &lt; 5; i++){
    exec.execute(new LiftOff1()); //&#x63D0;&#x4EA4;&#x4EFB;&#x52A1;
}

//shutdown&#x4F1A;&#x5173;&#x95ED;&#x7EBF;&#x7A0B;&#x6C60;&#x5165;&#x53E3;&#xFF0C;&#x4E0D;&#x80FD;&#x518D;&#x63D0;&#x4EA4;&#x65B0;&#x4EFB;&#x52A1;&#xFF0C;&#x4F46;&#x4E4B;&#x524D;&#x63D0;&#x4EA4;&#x7684;&#xFF0C;&#x4F1A;&#x6B63;&#x5E38;&#x8FD0;&#x884C;&#x5230;&#x7ED3;&#x675F;
//&#x5982;&#x679C;&#x4E0D;&#x5173;&#x95ED;&#xFF0C;&#x7EBF;&#x7A0B;&#x6C60;&#x4F1A;&#x4E00;&#x76F4;&#x5F00;&#x7740;&#xFF0C;&#x7B49;&#x5F85;&#x63D0;&#x4EA4;&#x4EFB;&#x52A1;&#xFF0C;&#x8FDB;&#x7A0B;&#x4E5F;&#x5C31;&#x4E0D;&#x4F1A;&#x5173;&#x95ED;
exec.shutdown();
</code></pre><p>&#x5B9A;&#x65F6;&#xFF0C;&#x5EF6;&#x65F6;--Schduled</p>
<pre><code>//&#x4F7F;&#x7528;newScheduledThreadPool&#x6765;&#x6A21;&#x62DF;&#x5FC3;&#x8DF3;&#x673A;&#x5236;
public class HeartBeat {
    public static void main(String[] args) {
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);  //5&#x662F;corePoolSize
        Runnable task = new Runnable() {
            public void run() {
                System.out.println(&quot;HeartBeat.........................&quot;);
            }
        };
        executor.scheduleAtFixedRate(task,5,3, TimeUnit.SECONDS);   //5&#x79D2;&#x540E;&#x7B2C;&#x4E00;&#x6B21;&#x6267;&#x884C;&#xFF0C;&#x4E4B;&#x540E;&#x6BCF;&#x9694;3&#x79D2;&#x6267;&#x884C;&#x4E00;&#x6B21;
    }
}
</code></pre><h3 id="12-threadfactory&#x548C;thread">1.2 ThreadFactory&#x548C;Thread</h3>
<p>&#x5148;&#x770B;ThreadFactory</p>
<pre><code>//&#x8BBE;&#x7F6E;ThreadFactory&#xFF1A;&#x53EA;&#x6709;&#x5F53;&#x9700;&#x8981;&#x65B0;&#x7EBF;&#x7A0B;&#x65F6;&#xFF0C;&#x624D;&#x4F1A;&#x6765;&#x8FD9;&#x91CC;&#x8C03;&#x7528;&#xFF0C;&#x5C31;&#x662F;&#x8BF4;ThreadFactory&#x672C;&#x8EAB;&#x4E0D;&#x7BA1;&#x7406;&#x7EBF;&#x7A0B;&#x6C60;&#xFF0C;&#x53EA;&#x662F;&#x7ED9;&#x7EBF;&#x7A0B;&#x6C60;&#x5E72;&#x6D3B;&#xFF08;&#x63D0;&#x4F9B;&#x65B0;&#x7EBF;&#x7A0B;&#xFF09;

ExecutorService exec = Executors.newFixedThreadPool(2, new ThreadFactory() {

    private int threadCount = 0;

    @Override
    public Thread newThread(Runnable r) {
        threadCount++;
        Thread tr = new Thread(r, &quot;thread-from-ThreadFactory-&quot; + threadCount);
        //&#x8FD9;&#x91CC;&#x53EF;&#x4EE5;&#x7ED9;&#x7EBF;&#x7A0B;&#x8BBE;&#x7F6E;&#x4E00;&#x4E9B;&#x5C5E;&#x6027;
        tr.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler(){  
            @Override  
            public void uncaughtException(Thread t, Throwable e) {  
                e.printStackTrace();
            }  
        });  
        //tr.setDaemon(true);    //&#x8BBE;&#x7F6E;&#x8FD9;&#x4E2A;&#x4E3A;true&#xFF0C;&#x5219;&#x4E3B;&#x7EBF;&#x7A0B;&#x9000;&#x51FA;&#x65F6;&#xFF0C;&#x5B50;&#x7EBF;&#x7A0B;&#x4E0D;&#x7BA1;&#x662F;&#x5426;&#x7ED3;&#x675F;&#xFF0C;&#x90FD;&#x9000;&#x51FA;
        tr.setPriority(Thread.MAX_PRIORITY);  

        return tr;
    }
});
</code></pre><p>Thread&#x91CC;&#x7684;&#x7EBF;&#x7A0B;&#x5C5E;&#x6027;&#xFF1A;</p>
<p><code>&#x540E;&#x53F0;&#x7EBF;&#x7A0B;&#xFF1A;</code><br>tr.setDaemon(true);      //&#x8BBE;&#x7F6E;&#x8FD9;&#x4E2A;&#x4E3A;true&#xFF0C;&#x5219;&#x4E3B;&#x7EBF;&#x7A0B;&#x9000;&#x51FA;&#x65F6;&#xFF0C;&#x5B50;&#x7EBF;&#x7A0B;&#x4E0D;&#x7BA1;&#x662F;&#x5426;&#x7ED3;&#x675F;&#xFF0C;&#x90FD;&#x9000;&#x51FA;
&#x540E;&#x53F0;&#x7EBF;&#x7A0B;&#x8868;&#x793A;&#x5728;&#x7A0B;&#x5E8F;&#x540E;&#x53F0;&#x63D0;&#x4F9B;&#x4E00;&#x79CD;&#x901A;&#x7528;&#x670D;&#x52A1;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x4E14;&#x4E0D;&#x662F;&#x7A0B;&#x5E8F;&#x4E0D;&#x53EF;&#x6216;&#x7F3A;&#x7684;&#x90E8;&#x5206;
&#x5F53;&#x6240;&#x6709;&#x975E;&#x540E;&#x53F0;&#x7EBF;&#x7A0B;&#x7ED3;&#x675F;&#x4E86;&#xFF0C;&#x540E;&#x53F0;&#x7EBF;&#x7A0B;&#x4E5F;&#x5C31;&#x7ED3;&#x675F;&#x4E86;
isDaemon()&#x5224;&#x65AD;&#x662F;&#x5426;&#x540E;&#x53F0;&#x7EBF;&#x7A0B;
&#x4ECE;&#x540E;&#x53F0;&#x7EBF;&#x7A0B;&#x521B;&#x5EFA;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x81EA;&#x52A8;&#x9ED8;&#x8BA4;&#x662F;&#x540E;&#x53F0;&#x7EBF;&#x7A0B;</p>
<p><code>&#x4F18;&#x5148;&#x7EA7;&#xFF1A;</code><br>tr.setPriority(Thread.MAX_PRIORITY);<br>&#x4EC5;&#x4EC5;&#x662F;&#x6267;&#x884C;&#x9891;&#x7387;&#x8F83;&#x4F4E;&#xFF0C;&#x4E0D;&#x4F1A;&#x9020;&#x6210;&#x6B7B;&#x9501;&#xFF08;&#x7EBF;&#x7A0B;&#x5F97;&#x4E0D;&#x5230;&#x6267;&#x884C;&#xFF09;
JDK&#x6709;&#x5341;&#x4E2A;&#x4F18;&#x5148;&#x7EA7;&#xFF0C;&#x4F46;&#x548C;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x6620;&#x5C04;&#x7684;&#x4E0D;&#x662F;&#x5F88;&#x597D;
windows&#x6709;7&#x4E2A;&#x4F18;&#x5148;&#x7EA7;&#xFF0C;&#x4F46;&#x4E0D;&#x56FA;&#x5B9A;
Sun&#x7684;Solaris&#x6709;2&#x7684;31&#x6B21;&#x65B9;&#x4E2A;&#x4F18;&#x5148;&#x7EA7;
&#x6240;&#x4EE5;&#x8C03;&#x7528;&#x4F18;&#x5148;&#x7EA7;&#x65F6;&#xFF0C;&#x5B89;&#x5168;&#x7684;&#x505A;&#x6CD5;&#x662F;&#x53EA;&#x4F7F;&#x7528;&#xFF1A;MAX_PRIORITY, NORM_PRIORITY, MIN_PRIORITY</p>
<p><code>&#x7EBF;&#x7A0B;&#x540D;&#x5B57;&#xFF1A;&#x53C2;&#x6570;2</code><br>Thread tr = new Thread(r, &quot;thread-name-&quot; + threadCount);  </p>
<p><code>&#x5168;&#x5C40;&#x5F02;&#x5E38;</code>
&#x5168;&#x5C40;&#x5F02;&#x5E38;&#x662F;&#x57FA;&#x4E8E;&#x7EBF;&#x7A0B;&#x7684;&#xFF0C;&#x5E76;&#x4E14;&#x5F02;&#x5E38;&#x4E0D;&#x80FD;&#x8DE8;&#x7EBF;&#x7A0B;&#x4F20;&#x9012;</p>
<pre><code>tr.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler(){  
    @Override  
    public void uncaughtException(Thread t, Throwable e) {  
        e.printStackTrace();
    }  
});  

&#x800C;Thread.setUncaughtExceptionHandler&#x662F;&#x7ED9;&#x6240;&#x6709;&#x7EBF;&#x7A0B;&#x90FD;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x5168;&#x5C40;&#x5F02;&#x5E38;&#x6355;&#x6349;
</code></pre><p><code>isAlive()&#xFF1A;&#x7EBF;&#x7A0B;&#x662F;&#x5426;&#x8FD8;&#x6D3B;&#x7740;</code>
&#x8FD9;&#x4E2A;&#x4F1A;&#x5F71;&#x54CD;join<br>run&#x65B9;&#x6CD5;&#x6267;&#x884C;&#x5B8C;&#x6BD5;&#xFF0C;&#x662F;&#x5426;isAlive&#xFF1F;<br>&#x7EBF;&#x7A0B;&#x88AB;&#x4E2D;&#x65AD;&#xFF0C;&#x662F;&#x5426;isAlive&#xFF1F;  </p>
<h3 id="13-&#x5173;&#x4E8E;threadpoolexecutor">1.3 &#x5173;&#x4E8E;ThreadPoolExecutor</h3>
<p>&#x4E0A;&#x9762;&#x63D0;&#x5230;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;&#x57FA;&#x672C;&#x7ED3;&#x6784;&#x90FD;&#x662F;&#x8FD9;&#x4E2A;&#xFF1A;</p>
<pre><code>public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue&lt;Runnable&gt; workQueue,
                          ThreadFactory threadFactory,  
                          RejectedExecutionHandler handler) //&#x540E;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x4E3A;&#x53EF;&#x9009;&#x53C2;&#x6570;

corePoolSize&#xFF1A; &#x7EBF;&#x7A0B;&#x6C60;&#x7EF4;&#x62A4;&#x7EBF;&#x7A0B;&#x7684;&#x6700;&#x5C11;&#x6570;&#x91CF;

maximumPoolSize&#xFF1A;&#x7EBF;&#x7A0B;&#x6C60;&#x7EF4;&#x62A4;&#x7EBF;&#x7A0B;&#x7684;&#x6700;&#x5927;&#x6570;&#x91CF;

keepAliveTime&#xFF1A; &#x7EBF;&#x7A0B;&#x6C60;&#x7EF4;&#x62A4;&#x7EBF;&#x7A0B;&#x6240;&#x5141;&#x8BB8;&#x7684;&#x7A7A;&#x95F2;&#x65F6;&#x95F4;&#xFF0C;&#x8D85;&#x65F6;&#x5219;&#x7EBF;&#x7A0B;&#x6B7B;&#xFF0C;&#x6B7B;&#x5230;&#x6700;&#x5C11;&#x6570;&#x91CF;corePoolSize

unit&#xFF1A; keepAliveTime&#x7684;&#x5355;&#x4F4D;

workQueue&#xFF1A; &#x7EBF;&#x7A0B;&#x6C60;&#x6240;&#x4F7F;&#x7528;&#x7684;&#x7F13;&#x51B2;&#x961F;&#x5217;

threadFactory&#xFF1A;&#x521B;&#x5EFA;&#x65B0;&#x7EBF;&#x7A0B;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x4F7F;&#x7528;ThreadFactory&#x521B;&#x5EFA;&#x65B0;&#x7EBF;&#x7A0B;&#xFF0C;&#x9ED8;&#x8BA4;&#x4F7F;&#x7528;defaultThreadFactory&#x521B;&#x5EFA;&#x7EBF;&#x7A0B;

handler&#xFF1A; &#x7EBF;&#x7A0B;&#x6C60;&#x5BF9;&#x62D2;&#x7EDD;&#x4EFB;&#x52A1;&#x7684;&#x5904;&#x7406;&#x7B56;&#x7565;
</code></pre><p>corePoolSize&#xFF1A;&#x6838;&#x5FC3;&#x7EBF;&#x7A0B;&#x6570;&#xFF0C;&#x5982;&#x679C;&#x8FD0;&#x884C;&#x7684;&#x7EBF;&#x7A0B;&#x5C11;&#x4E8E;corePoolSize&#xFF0C;&#x5219;&#x521B;&#x5EFA;&#x65B0;&#x7EBF;&#x7A0B;&#x6765;&#x6267;&#x884C;&#x65B0;&#x4EFB;&#x52A1;&#xFF0C;&#x5373;&#x4F7F;&#x7EBF;&#x7A0B;&#x6C60;&#x4E2D;&#x7684;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x662F;&#x7A7A;&#x95F2;&#x7684;  </p>
<p>maximumPoolSize:&#x6700;&#x5927;&#x7EBF;&#x7A0B;&#x6570;&#xFF0C;&#x53EF;&#x5141;&#x8BB8;&#x521B;&#x5EFA;&#x7684;&#x7EBF;&#x7A0B;&#x6570;&#xFF0C;corePoolSize&#x548C;maximumPoolSize&#x8BBE;&#x7F6E;&#x7684;&#x8FB9;&#x754C;&#x81EA;&#x52A8;&#x8C03;&#x6574;&#x6C60;&#x5927;&#x5C0F;&#xFF1A;<br>corePoolSize &lt;&#x8FD0;&#x884C;&#x7684;&#x7EBF;&#x7A0B;&#x6570;&lt; maximumPoolSize:&#x4EC5;&#x5F53;&#x961F;&#x5217;&#x6EE1;&#x65F6;&#x624D;&#x521B;&#x5EFA;&#x65B0;&#x7EBF;&#x7A0B;<br>corePoolSize=&#x8FD0;&#x884C;&#x7684;&#x7EBF;&#x7A0B;&#x6570;= maximumPoolSize&#xFF1A;&#x521B;&#x5EFA;&#x56FA;&#x5B9A;&#x5927;&#x5C0F;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;  </p>
<p>keepAliveTime:&#x5982;&#x679C;&#x7EBF;&#x7A0B;&#x6570;&#x591A;&#x4E8E;corePoolSize,&#x5219;&#x8FD9;&#x4E9B;&#x591A;&#x4F59;&#x7684;&#x7EBF;&#x7A0B;&#x7684;&#x7A7A;&#x95F2;&#x65F6;&#x95F4;&#x8D85;&#x8FC7;keepAliveTime&#x65F6;&#x5C06;&#x88AB;&#x7EC8;&#x6B62;  </p>
<p>unit:keepAliveTime&#x53C2;&#x6570;&#x7684;&#x65F6;&#x95F4;&#x5355;&#x4F4D;  </p>
<p>workQueue:&#x4FDD;&#x5B58;&#x4EFB;&#x52A1;&#x7684;&#x963B;&#x585E;&#x961F;&#x5217;&#xFF0C;&#x4E0E;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x5927;&#x5C0F;&#x6709;&#x5173;&#xFF1A;<br>  &#x5F53;&#x8FD0;&#x884C;&#x7684;&#x7EBF;&#x7A0B;&#x6570;&#x5C11;&#x4E8E;corePoolSize&#x65F6;&#xFF0C;&#x5728;&#x6709;&#x65B0;&#x4EFB;&#x52A1;&#x65F6;&#x76F4;&#x63A5;&#x521B;&#x5EFA;&#x65B0;&#x7EBF;&#x7A0B;&#x6765;&#x6267;&#x884C;&#x4EFB;&#x52A1;&#x800C;&#x65E0;&#x9700;&#x518D;&#x8FDB;&#x961F;&#x5217;<br>  &#x5F53;&#x8FD0;&#x884C;&#x7684;&#x7EBF;&#x7A0B;&#x6570;&#x7B49;&#x4E8E;&#x6216;&#x591A;&#x4E8E;corePoolSize&#xFF0C;&#x5728;&#x6709;&#x65B0;&#x4EFB;&#x52A1;&#x6DFB;&#x52A0;&#x65F6;&#x5219;&#x9009;&#x52A0;&#x5165;&#x961F;&#x5217;&#xFF0C;&#x4E0D;&#x76F4;&#x63A5;&#x521B;&#x5EFA;&#x7EBF;&#x7A0B;<br>  &#x5F53;&#x961F;&#x5217;&#x6EE1;&#x65F6;&#xFF0C;&#x5728;&#x6709;&#x65B0;&#x4EFB;&#x52A1;&#x65F6;&#x5C31;&#x521B;&#x5EFA;&#x65B0;&#x7EBF;&#x7A0B;  </p>
<p>threadFactory:&#x4F7F;&#x7528;ThreadFactory&#x521B;&#x5EFA;&#x65B0;&#x7EBF;&#x7A0B;&#xFF0C;&#x9ED8;&#x8BA4;&#x4F7F;&#x7528;defaultThreadFactory&#x521B;&#x5EFA;&#x7EBF;&#x7A0B;  </p>
<p>handle:&#x5B9A;&#x4E49;&#x5904;&#x7406;&#x88AB;&#x62D2;&#x7EDD;&#x4EFB;&#x52A1;&#x7684;&#x7B56;&#x7565;&#xFF0C;&#x9ED8;&#x8BA4;&#x4F7F;&#x7528;ThreadPoolExecutor.AbortPolicy,&#x4EFB;&#x52A1;&#x88AB;&#x62D2;&#x7EDD;&#x65F6;&#x5C06;&#x629B;&#x51FA;RejectExecutorException</p>
<p>&#x518D;&#x770B;Executors&#x91CC;&#x4E00;&#x5806;new&#x65B9;&#x6CD5;&#x600E;&#x4E48;&#x7528;&#x7684;&#xFF1A;</p>
<pre><code>public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  //&#x4F7F;&#x7528;&#x540C;&#x6B65;&#x961F;&#x5217;&#xFF0C;&#x5C06;&#x4EFB;&#x52A1;&#x76F4;&#x63A5;&#x63D0;&#x4EA4;&#x7ED9;&#x7EBF;&#x7A0B;
                                  new SynchronousQueue&lt;Runnable&gt;());
}


//&#x7EBF;&#x7A0B;&#x6C60;&#xFF1A;&#x6307;&#x5B9A;&#x7EBF;&#x7A0B;&#x4E2A;&#x6570;
public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  //&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x57FA;&#x4E8E;FIFO&#x6392;&#x5E8F;&#x7684;&#x963B;&#x585E;&#x961F;&#x5217;&#xFF0C;&#x5728;&#x6240;&#x6709;corePoolSize&#x7EBF;&#x7A0B;&#x90FD;&#x5FD9;&#x65F6;&#x65B0;&#x4EFB;&#x52A1;&#x5C06;&#x5728;&#x961F;&#x5217;&#x4E2D;&#x7B49;&#x5F85;
                                  new LinkedBlockingQueue&lt;Runnable&gt;());
}


//&#x5355;&#x7EBF;&#x7A0B;&#xFF1A;&#x57FA;&#x4E8E;&#x4E00;&#x4E2A;&#x56FA;&#x5B9A;&#x4E2A;&#x6570;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;&#xFF0C;&#x4E0D;&#x7BA1;&#x5728;&#x54EA;&#x91CC;&#xFF0C;&#x5B9E;&#x73B0;&#x4E32;&#x884C;&#x6267;&#x884C;&#xFF0C;&#x90FD;&#x662F;&#x57FA;&#x4E8E;&#x4E00;&#x4E2A;&#x5176;&#x4ED6;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;&#x548C;&#x4E00;&#x4E2A;&#x961F;&#x5217;
public static ExecutorService newSingleThreadExecutor() {
   return new FinalizableDelegatedExecutorService
                         //corePoolSize&#x548C;maximumPoolSize&#x90FD;&#x7B49;&#x4E8E;&#xFF0C;&#x8868;&#x793A;&#x56FA;&#x5B9A;&#x7EBF;&#x7A0B;&#x6C60;&#x5927;&#x5C0F;&#x4E3A;1
                        (new ThreadPoolExecutor(1, 1,
                                                0L, TimeUnit.MILLISECONDS,
                                                new LinkedBlockingQueue&lt;Runnable&gt;()));
}
</code></pre><p>&#x5206;&#x6790;&#xFF1A;</p>
<pre><code>private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
BlockingQueue&lt;Runnable&gt; workQueue;

public void execute(Runnable command) {
    if (command == null)
        throw new NullPointerException();
    /*
     * Proceed in 3 steps:
     *
     * 1. If fewer than corePoolSize threads are running, try to
     * start a new thread with the given command as its first
     * task.  The call to addWorker atomically checks runState and
     * workerCount, and so prevents false alarms that would add
     * threads when it shouldn&apos;t, by returning false.
     *
     * 2. If a task can be successfully queued, then we still need
     * to double-check whether we should have added a thread
     * (because existing ones died since last checking) or that
     * the pool shut down since entry into this method. So we
     * recheck state and if necessary roll back the enqueuing if
     * stopped, or start a new thread if there are none.
     *
     * 3. If we cannot queue task, then we try to add a new
     * thread.  If it fails, we know we are shut down or saturated
     * and so reject the task.
     */
    int c = ctl.get();

    //&#x5F53;&#x524D;&#x6B63;&#x5728;&#x5DE5;&#x4F5C;&#x7684;&#x7EBF;&#x7A0B;&#x6570; &lt; &#x5141;&#x8BB8;&#x7684;&#x7EBF;&#x7A0B;&#x6570;&#xFF0C;&#x5219;&#x521B;&#x5EFA;&#x65B0;&#x7EBF;&#x7A0B;&#xFF0C;&#x8FD0;&#x884C;task
    if (workerCountOf(c) &lt; corePoolSize) {
        if (addWorker(command, true))  ///&#x6709;&#x4E2A;Worker&#x5185;&#x90E8;&#x7C7B;&#xFF0C;&#x5185;&#x90E8;&#x4F1A;&#x8C03;&#x7528;ThreadFactory.newThread()
            return;
        c = ctl.get();
    }
    if (isRunning(c) &amp;&amp; workQueue.offer(command)) {   
        int recheck = ctl.get();
        if (! isRunning(recheck) &amp;&amp; remove(command))
            reject(command);             //&#x8C03;&#x7528;RejectedExecutionHandler&#x7684;handler.rejectedExecution(command, this);
        else if (workerCountOf(recheck) == 0)
            addWorker(null, false);   //return false;
    }
    else if (!addWorker(command, false))
        reject(command);  //handler.rejectedExecution(command, this);
}
</code></pre><h3 id="14-callable&#x548C;future&#x7684;&#x4F7F;&#x7528;">1.4 Callable&#x548C;Future&#x7684;&#x4F7F;&#x7528;</h3>
<p>Callable&#x548C;Future&#xFF1A;&#x8981;&#x7684;&#x662F;&#x90A3;&#x4E2A;call&#x65B9;&#x6CD5;&#xFF0C;future&#x91CC;&#x653E;&#x7684;&#x662F;&#x5B50;&#x7EBF;&#x7A0B;&#x7684;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#xFF0C;get&#x65B9;&#x6CD5;&#x4F1A;&#x963B;&#x585E;&#x7B49;&#x5F85;&#x8FD4;&#x56DE;&#xFF0C;&#x5C31;&#x662F;&#x7B49;call&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;</p>
<p>Runnable&#x4E0D;&#x4EA7;&#x751F;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;ExecutorService.execute(Runnable)&#xFF0C;&#x8D70;&#x7684;&#x662F;run&#x65B9;&#x6CD5;<br>Callable&#x4EA7;&#x751F;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;ExecutorService.submit(Callable)&#xFF0C;&#x8D70;&#x7684;&#x662F;call&#x65B9;&#x6CD5;</p>
<pre><code>&#x7528;&#x6CD5;1&#xFF1A;submit Callable and get a Future, block in future.get()

interface ArchiveSearcher { String search(String target); }
class App {
    ExecutorService executor = ...
    ArchiveSearcher searcher = ...

    void showSearch(final String target) throws InterruptedException {

      Future future = executor.submit(new Callable() {
          public String call() {
              return searcher.search(target);
          }}
      );

      displayOtherThings(); // do other things while searching

      try {
        displayText(future.get()); // use future---&#x5728;&#x8FD9;&#x91CC;&#x4F1A;&#x963B;&#x585E;&#x7B49;&#x5F85;
      } catch (ExecutionException ex) { cleanup(); return; }
    }
}

&#x7528;&#x6CD5;2&#xFF1A;execute a FutureTask, and get a 

FutureTask future = new FutureTask(new Callable&lt;String&gt;() {
        public String call() {
            return searcher.search(target);
        }
    }
);
executor.execute(future);

future.get()


&#x5173;&#x4E8E;Callable&#xFF1A;&#x80FD;&#x8FD4;&#x56DE;&#x5C31;&#x7ED9;&#x8FD4;&#x56DE;&#xFF0C;&#x4E0D;&#x80FD;&#x8FD4;&#x56DE;&#x5C31;&#x629B;&#x5F02;&#x5E38;
public interface Callable&lt;V&gt; {
    V call() throws Exception;
}


&#x5173;&#x4E8E;Future&#xFF1A;
public interface Future&lt;V&gt; {

    boolean cancel(boolean mayInterruptIfRunning);
    boolean isCancelled();
    boolean isDone();
    V get() throws InterruptedException, ExecutionException;
    V get(long timeout, TimeUnit unit)  throws InterruptedException, ExecutionException, TimeoutException;
}
</code></pre><p>1 boolean cancel(boolean mayInterruptIfRunning);</p>
<p>&#x53C2;&#x6570;&#x7684;&#x610F;&#x601D;&#xFF1A;<br>&#x6B63;&#x5728;&#x6267;&#x884C;&#x7684;task&#x662F;&#x5426;&#x5141;&#x8BB8;&#x6253;&#x65AD;&#xFF0C;&#x5982;&#x679C;&#x662F;true&#xFF0C;&#x4F1A;&#x6253;&#x65AD;&#xFF0C;&#x5982;&#x679C;false&#xFF0C;&#x5219;&#x5141;&#x8BB8;in-progress&#x7684;&#x4EFB;&#x52A1;&#x6267;&#x884C;&#x5B8C;</p>
<p>&#x4F55;&#x65F6;&#x5931;&#x8D25;&#xFF1A;<br>&#x5DF2;&#x7ECF;&#x8FD0;&#x884C;&#x5B8C;&#x7684;task<br>&#x5DF2;&#x7ECF;&#x88AB;cancel&#x8FC7;&#x7684;task<br>&#x65E0;&#x6CD5;&#x88AB;&#x4E2D;&#x65AD;&#x7684;&#x4EFB;&#x52A1;  </p>
<p>&#x600E;&#x4E48;&#x6210;&#x529F;&#xFF1A;<br>&#x8FD8;&#x6CA1;start&#x7684;&#x4EFB;&#x52A1;&#xFF0C;&#x6BD4;&#x5982;&#x5728;&#x7B49;&#x5F85;&#x7684;&#xFF0C;&#x53EF;&#x4EE5;cancel
&#x6B63;&#x5728;running&#x7684;&#x4EFB;&#x52A1;&#xFF0C;&#x53C2;&#x6570;mayInterruptIfRunning&#x6307;&#x5B9A;&#x4E86;&#x662F;&#x4E0D;&#x662F;&#x53EF;&#x4EE5;&#x5C1D;&#x8BD5;interrupt</p>
<p>&#x526F;&#x4F5C;&#x7528;&#xFF1A;<br>&#x53EA;&#x8981;cancel&#x88AB;&#x8C03;&#x7528;&#x4E86;&#x4E14;&#x8FD4;&#x56DE;true&#xFF0C;isDone&#x548C;isCancelled&#x4E00;&#x76F4;&#x8FD4;&#x56DE;true</p>
<p>2 get&#xFF1A;&#x53D6;&#x56DE;&#x7ED3;&#x679C;&#xFF0C;&#x5982;&#x6709;&#x5FC5;&#x8981;&#xFF0C;&#x53EF;&#x4EE5;&#x963B;&#x585E;</p>
<p>&#x53EF;&#x4EE5;&#x963B;&#x585E;&#x5C31;&#x53EF;&#x4EE5;&#x88AB;interrupt<br>get()&#x6CA1;&#x6709;&#x8D85;&#x65F6;&#x65F6;&#x95F4;<br>get(1, TimeUnit.Second)&#xFF1A;&#x8868;&#x793A;&#x6700;&#x591A;&#x963B;&#x585E;1&#x79D2;&#xFF0C;&#x8FC7;&#x4E86;&#x4E00;&#x79D2;&#x5C31;&#x629B;&#x51FA;&#x8D85;&#x65F6;&#x5F02;&#x5E38;    </p>
<h3 id="15-&#x7EBF;&#x7A0B;&#x6C60;&#x6E90;&#x7801;&#x7B80;&#x5355;&#x5206;&#x6790;executor&#x63A5;&#x53E3;">1.5 &#x7EBF;&#x7A0B;&#x6C60;&#x6E90;&#x7801;&#x7B80;&#x5355;&#x5206;&#x6790;:Executor&#x63A5;&#x53E3;</h3>
<pre><code>public interface Executor {
    void execute(Runnable command);
}
</code></pre><p>&#x610F;&#x601D;&#x5C31;&#x662F;&#x7ED9;&#x4F60;&#x4E00;&#x4E2A;command&#xFF0C;&#x4F60;&#x60F3;&#x8BA9;&#x5B83;&#x5728;&#x54EA;&#x513F;&#x6267;&#x884C;run  </p>
<p>Excutor&#x80FD;&#x51B3;&#x5B9A;&#x7684;&#x4E8B;&#xFF1A;</p>
<ul>
<li>&#x9009;&#x62E9;&#x54EA;&#x4E2A;&#x7EBF;&#x7A0B;</li>
<li>&#x6267;&#x884C;runnable</li>
</ul>
<p>Excutor&#x7BA1;&#x4E0D;&#x4E86;&#x7684;&#x4E8B;&#xFF1A;</p>
<ul>
<li>Callable&#xFF0C;Future&#x7BA1;&#x4E0D;&#x4E86;</li>
<li>&#x6CA1;&#x6709;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x6C60;&#xFF0C;&#x7EBF;&#x7A0B;&#x6C60;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x81EA;&#x5DF1;&#x5199;&#xFF0C;&#x8DDF;Executor&#x6CA1;&#x5173;&#x7CFB;</li>
<li>&#x6CA1;&#x6CD5;&#x5EF6;&#x65F6;&#xFF0C;&#x5B9A;&#x65F6;&#x7684;&#x8FD0;&#x884C;&#x4EFB;&#x52A1;</li>
</ul>
<p>&#x4F8B;&#x5B50;&#xFF1A;
&#x770B;&#x4E0B;&#x9762;&#x4EE3;&#x7801;&#x91CC;&#x7684;&#x4E09;&#x4E2A;Executor&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x53D6;&#x81EA;java&#x6E90;&#x7801;&#x91CC;&#x7684;&#x6CE8;&#x91CA;&#xFF0C;&#x8FD9;&#x51E0;&#x884C;&#x4EE3;&#x7801;&#x57FA;&#x672C;&#x9610;&#x660E;&#x4E86;Executor&#x7684;&#x4F5C;&#x7528;</p>
<pre><code>public class C2 {

    public static class MyExecutors{

        public static Executor newDirectThreadPool(){
            return new DirectExecutor();
        }

        public static Executor newPerTaskPerThreadThreadPool(){
            return new ThreadPerTaskExecutor();
        }

        public static Executor newSerialThreadPool(){
            return new SerialExecutor(new DirectExecutor());
        }
    }

    /**
     * an executor can run the submitted task immediately in the caller&apos;s thread
     */
    public static class DirectExecutor implements Executor {
        public void execute(Runnable r) {
            r.run();
        }
    }

    /**
     * spawns a new thread for each task
     */
    public static class ThreadPerTaskExecutor implements Executor {
        public void execute(Runnable r) {
            new Thread(r).start();
        }
    }

    /**
     * serializes the submission of tasks to a second executor
     * &#x7C7B;&#x4F3C;&#x5B89;&#x5353;&#x7684;AsyncTask&#x91CC;&#x7684;&#x4E32;&#x884C;&#x5316;&#x5B9E;&#x73B0;
     */
    public static class SerialExecutor implements Executor {
        final Queue&lt;Runnable&gt; tasks = new ArrayDeque&lt;&gt;();
        final Executor executor;
        Runnable active;

        SerialExecutor(Executor executor) {
          this.executor = executor;
        }

        public synchronized void execute(final Runnable r) {
          tasks.add(new Runnable() {
            public void run() {
              try {
                r.run();
              } finally {
                scheduleNext();
              }
            }
          });
          if (active == null) {
            scheduleNext();
          }
        }

        protected synchronized void scheduleNext() {
          if ((active = tasks.poll()) != null) {
            executor.execute(active);
          }
        }
      }

    public static void main(String[] args) {
        Runnable task = new Runnable() {
            public void run() {
                try {
                    Thread.sleep(2000);
                    System.out.println(&quot;running on thread &quot; + Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        MyExecutors.newDirectThreadPool().execute(task);
        MyExecutors.newPerTaskPerThreadThreadPool().execute(task);
        MyExecutors.newSerialThreadPool().execute(task);

    }

}
</code></pre><h3 id="16-&#x7EBF;&#x7A0B;&#x6C60;&#x6E90;&#x7801;&#x7B80;&#x5355;&#x5206;&#x6790;executorservice&#x63A5;&#x53E3;">1.6 &#x7EBF;&#x7A0B;&#x6C60;&#x6E90;&#x7801;&#x7B80;&#x5355;&#x5206;&#x6790;:ExecutorService&#x63A5;&#x53E3;</h3>
<p>ThreadPoolExecutor extends AbstractExecutorService  &#x7BA1;&#x7684;&#x662F;&#x7EBF;&#x7A0B;&#x6C60;
AbstractExecutorService extends ExecutorService     &#x7BA1;&#x7684;&#x662F;&#x4EFB;&#x52A1;&#x542F;&#x52A8;&#xFF0C;&#x5173;&#x95ED;&#xFF0C;&#x8FD4;&#x56DE;
ExecutorService extends Executor  &#x53EA;&#x662F;interface</p>
<p>&#x800C;ExecutorService&#x6267;&#x884C;&#x4EFB;&#x52A1;&#x7684;&#x65B9;&#x5F0F;&#x6709;&#x4EE5;&#x4E0B;&#x4E09;&#x79CD;&#xFF1A;<br>exec.execute(runnable)<br>exec.execute(FutureTask)<br>Future<result> future = exec.submit(Callable)  </result></p>
<p>ExecutorService&#x63A5;&#x53E3;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>public interface ExecutorService extends Executor {
    void shutdown();
    List&lt;Runnable&gt; shutdownNow();
    boolean isShutdown();
    boolean isTerminated();
    boolean awaitTermination(long timeout, TimeUnit unit)
        throws InterruptedException;
    &lt;T&gt; Future&lt;T&gt; submit(Callable&lt;T&gt; task);
    &lt;T&gt; Future&lt;T&gt; submit(Runnable task, T result);
    Future&lt;?&gt; submit(Runnable task);
    &lt;T&gt; List&lt;Future&lt;T&gt;&gt; invokeAll(Collection&lt;? extends Callable&lt;T&gt;&gt; tasks)
        throws InterruptedException;
    &lt;T&gt; List&lt;Future&lt;T&gt;&gt; invokeAll(Collection&lt;? extends Callable&lt;T&gt;&gt; tasks,
                                  long timeout, TimeUnit unit)
        throws InterruptedException;

    &lt;T&gt; T invokeAny(Collection&lt;? extends Callable&lt;T&gt;&gt; tasks)
        throws InterruptedException, ExecutionException;
    &lt;T&gt; T invokeAny(Collection&lt;? extends Callable&lt;T&gt;&gt; tasks,
                    long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException;
}
</code></pre><p>&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#xFF0C;&#x4E00;&#x4E2A;ExecutorService&#x77E5;&#x9053;&#x5982;&#x4F55;&#x542F;&#x52A8;&#x4EFB;&#x52A1;&#xFF0C;&#x77E5;&#x9053;&#x5982;&#x4F55;&#x8FD4;&#x56DE;&#x4EFB;&#x52A1;&#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x77E5;&#x9053;&#x5982;&#x4F55;&#x5173;&#x95ED;&#x4EFB;&#x52A1;</p>
<p>&#x8981;&#x5F80;&#x4E0B;&#x8BF4;ExecutorService&#x600E;&#x4E48;&#x6267;&#x884C;&#x4EFB;&#x52A1;&#xFF0C;&#x5148;&#x56DE;&#x5934;&#x770B;&#x770B;&#x6240;&#x80FD;&#x6267;&#x884C;&#x7684;&#x4EFB;&#x52A1;&#xFF0C;&#x5206;&#x4E09;&#x79CD;&#xFF1A;<br>exec.execute(Runnable)<br>exec.execute(FutureTask)  &#x5176;&#x5B9E;&#x5C31;&#x662F;execute(Runnable)<br>Future<result> future = exec.submit(Callable)    </result></p>
<p>&#x8DDF;&#x7740;&#x4E0B;&#x9762;&#x7684;&#x6E90;&#x7801;&#xFF0C;&#x5F88;&#x5BB9;&#x6613;&#x5C31;&#x7406;&#x6E05;&#x8FD9;&#x4EE8;&#x7684;&#x5173;&#x7CFB;&#x4E86;</p>
<pre><code>public interface Runnable {
    V run();
}

public interface Callable&lt;V&gt; {
    V call() throws Exception;
}


&#x5173;&#x4E8E;Future&#xFF1A;
public interface Future&lt;V&gt; {

    boolean cancel(boolean mayInterruptIfRunning);
    boolean isCancelled();
    boolean isDone();
    V get() throws InterruptedException, ExecutionException;
    V get(long timeout, TimeUnit unit)  throws InterruptedException, ExecutionException, TimeoutException;
}

public interface RunnableFuture&lt;V&gt; extends Runnable, Future&lt;V&gt; {
    /**
     * Sets this Future to the result of its computation
     * unless it has been cancelled.
     */
    void run();
}


public class FutureTask&lt;V&gt; implements RunnableFuture&lt;V&gt;{

}

FutureTask&#x7684;&#x521D;&#x59CB;&#x5316;&#xFF1A;
FutureTask(Callable&lt;V&gt; callable)
FutureTask(Runnable runnable, V result)
&#x6CE8;&#x610F;&#xFF1A;Runnable + &#x8FD4;&#x56DE;&#x503C;&#x5C31;&#x662F;&#x4E00;&#x4E2A;Callable&#x4E86;&#x554A;&#xFF0C;&#x5177;&#x4F53;&#x770B;RunnableAdapter

&#x603B;&#x4E4B;&#xFF0C;FutureTask&#x5185;&#x90E8;&#x5C31;&#x6709;&#x4E86;&#x4E00;&#x4E2A;Callable

&#x5173;&#x4E8E;&#xFF1A;FutureTask(Runnable runnable, V result)
&#x8C03;&#x7528;&#x4E86;&#xFF1A;Executors.callable()
public static &lt;T&gt; Callable&lt;T&gt; callable(Runnable task, T result) {
    if (task == null)
        throw new NullPointerException();
    return new RunnableAdapter&lt;T&gt;(task, result);
}

static final class RunnableAdapter&lt;T&gt; implements Callable&lt;T&gt; {
    final Runnable task;
    final T result;
    RunnableAdapter(Runnable task, T result) {
        this.task = task;
        this.result = result;
    }
    public T call() {
        task.run();
        return result;
    }
}
</code></pre><p>&#x4E0B;&#x9762;&#x5C31;&#x53EF;&#x4EE5;&#x770B;&#x770B;ExecutorService.submit()&#x548C;execute&#x65B9;&#x6CD5;&#x4E86;</p>
<pre><code>public Future&lt;?&gt; submit(Runnable task) {
    if (task == null) throw new NullPointerException();
    RunnableFuture&lt;Void&gt; ftask = newTaskFor(task, null);
    execute(ftask);
    return ftask;
}

public &lt;T&gt; Future&lt;T&gt; submit(Runnable task, T result) {
    if (task == null) throw new NullPointerException();
    RunnableFuture&lt;T&gt; ftask = newTaskFor(task, result);
    execute(ftask);
    return ftask;
}

public &lt;T&gt; Future&lt;T&gt; submit(Callable&lt;T&gt; task) {
    if (task == null) throw new NullPointerException();
    RunnableFuture&lt;T&gt; ftask = newTaskFor(task);
    execute(ftask);
    return ftask;
}

protected &lt;T&gt; RunnableFuture&lt;T&gt; newTaskFor(Runnable runnable, T value) {
    return new FutureTask&lt;T&gt;(runnable, value);
}

protected &lt;T&gt; RunnableFuture&lt;T&gt; newTaskFor(Callable&lt;T&gt; callable) {
    return new FutureTask&lt;T&gt;(callable);
}
</code></pre><p>&#x6700;&#x540E;&#x90FD;&#x5F52;&#x7ED3;&#x5230;&#x4E86;execute(FutureTask)&#xFF0C;&#x5176;&#x5B9E;&#x5C31;&#x662F;execute(Runnable command)&#xFF0C;&#x8FD9;&#x4E2A;&#x5728;ThreadPoolExecutor&#x91CC;&#x5B9E;&#x73B0;&#x4E86;</p>
<pre><code>public void execute(Runnable command) {
    if (command == null)
        throw new NullPointerException();
    /*
     * Proceed in 3 steps:
     *
     * 1. If fewer than corePoolSize threads are running, try to
     * start a new thread with the given command as its first
     * task.  The call to addWorker atomically checks runState and
     * workerCount, and so prevents false alarms that would add
     * threads when it shouldn&apos;t, by returning false.
     *
     * 2. If a task can be successfully queued, then we still need
     * to double-check whether we should have added a thread
     * (because existing ones died since last checking) or that
     * the pool shut down since entry into this method. So we
     * recheck state and if necessary roll back the enqueuing if
     * stopped, or start a new thread if there are none.
     *
     * 3. If we cannot queue task, then we try to add a new
     * thread.  If it fails, we know we are shut down or saturated
     * and so reject the task.
     */
    int c = ctl.get();
    if (workerCountOf(c) &lt; corePoolSize) {
        if (addWorker(command, true))
            return;
        c = ctl.get();
    }
    if (isRunning(c) &amp;&amp; workQueue.offer(command)) {
        int recheck = ctl.get();
        if (! isRunning(recheck) &amp;&amp; remove(command))
            reject(command);
        else if (workerCountOf(recheck) == 0)
            addWorker(null, false);
    }
    else if (!addWorker(command, false))
        reject(command);
}
</code></pre><p>&#x6240;&#x4EE5;&#xFF0C;&#x5176;&#x5B9E;Future.get&#x7684;&#x963B;&#x585E;&#xFF0C;&#x4E0D;&#x662F;&#x7EBF;&#x7A0B;&#x6C60;&#x8D1F;&#x8D23;&#x7684;&#xFF0C;&#x800C;&#x662F;&#x5728;submit&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x7684;RunnableFuture&#x91CC;&#xFF0C;RunnableFuture&#x7684;&#x5B9E;&#x73B0;&#x7C7B;&#x5C31;&#x662F;FutureTask</p>
<pre><code class="lang-java"><span class="hljs-comment">///&#x8FD9;&#x5C31;&#x662F;Runnable&#x7684;run&#x65B9;&#x6CD5;</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">if</span> (state != NEW ||
        !U.compareAndSwapObject(<span class="hljs-keyword">this</span>, RUNNER, <span class="hljs-keyword">null</span>, Thread.currentThread()))
        <span class="hljs-keyword">return</span>;
    <span class="hljs-keyword">try</span> {
        Callable&lt;V&gt; c = callable;
        <span class="hljs-keyword">if</span> (c != <span class="hljs-keyword">null</span> &amp;&amp; state == NEW) {
            V result;
            <span class="hljs-keyword">boolean</span> ran;
            <span class="hljs-keyword">try</span> {
                result = c.call();
                ran = <span class="hljs-keyword">true</span>;
            } <span class="hljs-keyword">catch</span> (Throwable ex) {
                result = <span class="hljs-keyword">null</span>;
                ran = <span class="hljs-keyword">false</span>;
                setException(ex);
            }
            <span class="hljs-keyword">if</span> (ran)
                set(result);
        }
    } <span class="hljs-keyword">finally</span> {
        <span class="hljs-comment">// runner must be non-null until state is settled to</span>
        <span class="hljs-comment">// prevent concurrent calls to run()</span>
        runner = <span class="hljs-keyword">null</span>;
        <span class="hljs-comment">// state must be re-read after nulling runner to prevent</span>
        <span class="hljs-comment">// leaked interrupts</span>
        <span class="hljs-keyword">int</span> s = state;
        <span class="hljs-keyword">if</span> (s &gt;= INTERRUPTING)
            handlePossibleCancellationInterrupt(s);
    }
}

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">cancel</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> mayInterruptIfRunning)</span> </span>{
    <span class="hljs-keyword">if</span> (!(state == NEW &amp;&amp;
          U.compareAndSwapInt(<span class="hljs-keyword">this</span>, STATE, NEW,
              mayInterruptIfRunning ? INTERRUPTING : CANCELLED)))
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
    <span class="hljs-keyword">try</span> {    <span class="hljs-comment">// in case call to interrupt throws exception</span>
        <span class="hljs-keyword">if</span> (mayInterruptIfRunning) {
            <span class="hljs-keyword">try</span> {
                Thread t = runner;
                <span class="hljs-keyword">if</span> (t != <span class="hljs-keyword">null</span>)
                    t.interrupt();
            } <span class="hljs-keyword">finally</span> { <span class="hljs-comment">// final state</span>
                U.putOrderedInt(<span class="hljs-keyword">this</span>, STATE, INTERRUPTED);
            }
        }
    } <span class="hljs-keyword">finally</span> {
        finishCompletion();
    }
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
}

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@throws</span> CancellationException {<span class="hljs-doctag">@inheritDoc</span>}
 */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> V <span class="hljs-title">get</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> InterruptedException, ExecutionException </span>{
    <span class="hljs-keyword">int</span> s = state;
    <span class="hljs-keyword">if</span> (s &lt;= COMPLETING)
        s = awaitDone(<span class="hljs-keyword">false</span>, <span class="hljs-number">0L</span>);
    <span class="hljs-keyword">return</span> report(s);
}

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@throws</span> CancellationException {<span class="hljs-doctag">@inheritDoc</span>}
 */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> V <span class="hljs-title">get</span><span class="hljs-params">(<span class="hljs-keyword">long</span> timeout, TimeUnit unit)</span>
    <span class="hljs-keyword">throws</span> InterruptedException, ExecutionException, TimeoutException </span>{
    <span class="hljs-keyword">if</span> (unit == <span class="hljs-keyword">null</span>)
        <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> NullPointerException();
    <span class="hljs-keyword">int</span> s = state;
    <span class="hljs-keyword">if</span> (s &lt;= COMPLETING &amp;&amp;
        (s = awaitDone(<span class="hljs-keyword">true</span>, unit.toNanos(timeout))) &lt;= COMPLETING)
        <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> TimeoutException();
    <span class="hljs-keyword">return</span> report(s);
}

 <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> <span class="hljs-title">awaitDone</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> timed, <span class="hljs-keyword">long</span> nanos)</span>
    <span class="hljs-keyword">throws</span> InterruptedException </span>{
    <span class="hljs-comment">// The code below is very delicate, to achieve these goals:</span>
    <span class="hljs-comment">// - call nanoTime exactly once for each call to park</span>
    <span class="hljs-comment">// - if nanos &lt;= 0, return promptly without allocation or nanoTime</span>
    <span class="hljs-comment">// - if nanos == Long.MIN_VALUE, don&apos;t underflow</span>
    <span class="hljs-comment">// - if nanos == Long.MAX_VALUE, and nanoTime is non-monotonic</span>
    <span class="hljs-comment">//   and we suffer a spurious wakeup, we will do no worse than</span>
    <span class="hljs-comment">//   to park-spin for a while</span>
    <span class="hljs-keyword">long</span> startTime = <span class="hljs-number">0L</span>;    <span class="hljs-comment">// Special value 0L means not yet parked</span>
    WaitNode q = <span class="hljs-keyword">null</span>;
    <span class="hljs-keyword">boolean</span> queued = <span class="hljs-keyword">false</span>;
    <span class="hljs-keyword">for</span> (;;) {
        <span class="hljs-keyword">int</span> s = state;
        <span class="hljs-keyword">if</span> (s &gt; COMPLETING) {
            <span class="hljs-keyword">if</span> (q != <span class="hljs-keyword">null</span>)
                q.thread = <span class="hljs-keyword">null</span>;
            <span class="hljs-keyword">return</span> s;
        }
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (s == COMPLETING)
            <span class="hljs-comment">// We may have already promised (via isDone) that we are done</span>
            <span class="hljs-comment">// so never return empty-handed or throw InterruptedException</span>
            Thread.yield();
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (Thread.interrupted()) {
            removeWaiter(q);
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> InterruptedException();
        }
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (q == <span class="hljs-keyword">null</span>) {
            <span class="hljs-keyword">if</span> (timed &amp;&amp; nanos &lt;= <span class="hljs-number">0L</span>)
                <span class="hljs-keyword">return</span> s;
            q = <span class="hljs-keyword">new</span> WaitNode();
        }
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (!queued)
            queued = U.compareAndSwapObject(<span class="hljs-keyword">this</span>, WAITERS,
                                            q.next = waiters, q);
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (timed) {
            <span class="hljs-keyword">final</span> <span class="hljs-keyword">long</span> parkNanos;
            <span class="hljs-keyword">if</span> (startTime == <span class="hljs-number">0L</span>) { <span class="hljs-comment">// first time</span>
                startTime = System.nanoTime();
                <span class="hljs-keyword">if</span> (startTime == <span class="hljs-number">0L</span>)
                    startTime = <span class="hljs-number">1L</span>;
                parkNanos = nanos;
            } <span class="hljs-keyword">else</span> {
                <span class="hljs-keyword">long</span> elapsed = System.nanoTime() - startTime;
                <span class="hljs-keyword">if</span> (elapsed &gt;= nanos) {
                    removeWaiter(q);
                    <span class="hljs-keyword">return</span> state;
                }
                parkNanos = nanos - elapsed;
            }
            <span class="hljs-comment">// nanoTime may be slow; recheck before parking</span>
            <span class="hljs-keyword">if</span> (state &lt; COMPLETING)
                LockSupport.parkNanos(<span class="hljs-keyword">this</span>, parkNanos);
        }
        <span class="hljs-keyword">else</span>
            LockSupport.park(<span class="hljs-keyword">this</span>);
    }
}

<span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">WaitNode</span> </span>{
    <span class="hljs-keyword">volatile</span> Thread thread;
    <span class="hljs-keyword">volatile</span> WaitNode next;
    WaitNode() { thread = Thread.currentThread(); }
}
</code></pre>
<hr>
<p>&#x6700;&#x540E;&#xFF0C;FutureTask&#x5B9E;&#x73B0;&#x7684;&#x662F;RunnableFuture&#xFF0C;&#x5176;&#x5B9E;&#x4F60;&#x5B8C;&#x5168;&#x53EF;&#x4EE5;&#x4E0D;&#x7528;FutureTask&#xFF0C;
&#x4F8B;&#x5982;&#x4F60;&#x60F3;&#x5B9E;&#x73B0;&#x4E2A;MyFutureTask&#xFF0C;&#x8FD9;&#x4E2A;&#x4E0D;&#x4F1A;&#x5728;get&#x65B9;&#x6CD5;&#x91CC;&#x963B;&#x585E;&#xFF0C;&#x800C;&#x662F;&#x57FA;&#x4E8E;&#x5F02;&#x6B65;IO&#xFF0C;</p>
<pre><code>public class CustomThreadPoolExecutor extends ThreadPoolExecutor {

    static class CustomTask implements RunnableFuture {...}

    protected  RunnableFuture newTaskFor(Callable c) {
        return new CustomTask(c);
    }
    protected  RunnableFuture newTaskFor(Runnable r, V v) {
        return new CustomTask(r, v);
    }
    // ... add constructors, etc.
  }
</code></pre>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../concurrent.html" class="navigation navigation-prev " aria-label="Previous page: 线程和并发">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="block-close.html" class="navigation navigation-next " aria-label="Next page: 线程阻塞和关闭">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"线程和线程池","level":"1.5.1","depth":2,"next":{"title":"线程阻塞和关闭","level":"1.5.2","depth":2,"path":"concurrent/block-close.md","ref":"concurrent/block-close.md","articles":[]},"previous":{"title":"线程和并发","level":"1.5","depth":1,"path":"concurrent.md","ref":"concurrent.md","articles":[{"title":"线程和线程池","level":"1.5.1","depth":2,"path":"concurrent/how-to.md","ref":"concurrent/how-to.md","articles":[]},{"title":"线程阻塞和关闭","level":"1.5.2","depth":2,"path":"concurrent/block-close.md","ref":"concurrent/block-close.md","articles":[]},{"title":"线程协作","level":"1.5.3","depth":2,"path":"concurrent/communicate.md","ref":"concurrent/communicate.md","articles":[]},{"title":"活性失败-volatile","level":"1.5.4","depth":2,"path":"concurrent/volatile.md","ref":"concurrent/volatile.md","articles":[]},{"title":"安全性失败-锁","level":"1.5.5","depth":2,"path":"concurrent/lock.md","ref":"concurrent/lock.md","articles":[]},{"title":"线程局部存储","level":"1.5.6","depth":2,"path":"concurrent/thread-local.md","ref":"concurrent/thread-local.md","articles":[]},{"title":"其他","level":"1.5.7","depth":2,"path":"concurrent/others.md","ref":"concurrent/others.md","articles":[]}]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"concurrent/how-to.md","mtime":"2017-04-09T06:00:05.357Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-04-09T09:50:22.406Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

